<template>
	<!-- <button v-if="haveAuth" class="slot-btn" type="default" open-type="getUserInfo" @getuserinfo="getAuthorize" withCredentials="true" hover-class="none">
		<slot></slot>
	</button> -->
	<button v-if="haveAuth" class="slot-btn" @click="getAuthorize" hover-class="none">
		<slot></slot>
	</button>
	<!--  -->
	<view v-else @click="nextClick">
		<slot></slot>
	</view>
</template>

<script>
import { toast } from '../../utils/util';
import {updateUserinfo} from '../../tool/userTool'
	let app = getApp();

	export default {
		name: 'wx-auth',
		props: {
			haveAuth: {
				type: Boolean,
				default(){
					return false
				}
			},
			callBack: {
				type: String,
				default(){
					return ""
				}
			}
		},

		data() {
			return {

			};
		},
		methods: {


			getAuthorize(e){
				uni.getUserProfile({
                   desc:'Wexin',     // 这个参数是必须的
                   success:res=>{
					   let anthOk = false;
						if(!res.encryptedData){
							toast('请授权头像')
							uni.$emit(this.callBack,{anthOk:anthOk})
						}else{

							let wxUserInfo = res.userInfo;
							updateUserinfo({
								params:{
									nickname: wxUserInfo.nickName,
									headimgurl: wxUserInfo.avatarUrl
								}
							}).then(res => {
								app.globalData.wxUserInfo = wxUserInfo;
								uni.setStorageSync('wxUserInfo', wxUserInfo);
								anthOk = true;
								uni.$emit(this.callBack,{anthOk:anthOk})
								toast('请再次点击，授权手机号')
							})
						}
						},
						fail:err=>{
							console.log(err)
						}
					})

				// console.log(e)
				// console.log('什么鬼')
				// console.log(e.mp.detail.encryptedData)
				// let anthOk = false;
				// if(!e.mp.detail.encryptedData){
				// 	toast('请授权头像')
				// 	uni.$emit(this.callBack,{anthOk:anthOk})
				// }else{

				// 	let wxUserInfo = e.mp.detail.userInfo;
				// 	updateUserinfo({
				// 		params:{
				// 			nickname: wxUserInfo.nickName,
				// 			headimgurl: wxUserInfo.avatarUrl
				// 		}
				// 	}).then(res => {
				// 		app.globalData.wxUserInfo = wxUserInfo;
				// 		uni.setStorageSync('wxUserInfo', wxUserInfo);
				// 		anthOk = true;
				// 		uni.$emit(this.callBack,{anthOk:anthOk})
				// 		toast('请再次点击，授权手机号')
				// 	})
				// }

			},
			nextClick(){
				uni.$emit(this.callBack,{anthOk:false})
			}
		}

	}
</script>

<style lang="scss">
.slot-btn{
		background-color: rgba(0,0,0,0)!important;
		padding: 0;margin: 0;

		position: none;
		display: block;

		&::after {
			border: none;


		}
		&.button-hover {
			color:rgba(0, 0, 0, 0.6);
			background-color:#fff;
			transform: translate(1rpx, 1rpx);
		}
}

</style>
